using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;

namespace Common.Shared.IO {
  public class DriveCollection : List<LogicalDriveInfo> {
    public DriveCollection ( ) {
      
    }

    public LogicalDriveInfo this[ char drive ] {
      get {
        if ( Contains ( drive ) )
          return this[ IndexOf ( drive ) ];
        else
          return null;
      }
    }

    public int IndexOf ( char drive ) {
      if ( char.IsLetter ( drive ) ) {
        for ( int i = 0; i < this.Count; i++ ) {
          LogicalDriveInfo drinfo = this[ i ];
          if ( char.ToLower ( drinfo.Name[0] ) == char.ToLower ( drive ) )
            return i;
        }
      }
      return -1;
    }

    /// <summary>
    /// Determines whether the system contains the specified drive letter.
    /// </summary>
    /// <param name="drive">The drive.</param>
    /// <returns>
    /// 	<c>true</c> if the system contains the specified drive letter; otherwise, <c>false</c>.
    /// </returns>
    public bool Contains ( char drive ) {
      return this.IndexOf ( drive ) >= 0;
    }

    public void Load ( ) {
      foreach ( LogicalDriveInfo ldi in LogicalDriveInfo.GetDrives ( ) ) {
        if(!this.Contains(ldi.Letter))
          this.Add ( ldi );
      }
    }

    public void Load ( char letter ) {
      if ( !this.Contains ( letter ) ) {
        this.Add ( new LogicalDriveInfo ( letter ) );
      }
    }

    #region hidden methods
    private new void Add ( LogicalDriveInfo drive ) {
      base.Add ( drive );
    }

    private new void AddRange ( IEnumerable<LogicalDriveInfo> drives ) {
      base.AddRange ( drives );
    }

    private new void Clear ( ) {
      base.Clear ( );
    }

    private new void Insert ( int index, LogicalDriveInfo drive ) {
      base.Insert ( index, drive );
    }

    private new void InsertRange ( int index, IEnumerable<LogicalDriveInfo> drives ) {
      base.InsertRange ( index, drives );
    }

    internal new void Remove ( LogicalDriveInfo drive ) {
      base.Remove ( drive );
    }

    private new void RemoveAt ( int index ) {
      base.RemoveAt ( index );
    }

    private new void RemoveAll ( Predicate<LogicalDriveInfo> match ) {
      base.RemoveAll ( match );
    }

    private new void RemoveRange ( int index, int count ) {
      base.RemoveRange ( index, count );
    }
    #endregion
  }
}
